<template>
	<view class="mainbgc">
		<view class="header-top-box">
			<view class="header-top fff flexbetween">
				<view class="top-image" @click="gotoBack()"></view>
				<view class="searchinput flexleft">
					<view class="searchimg"></view>
					<input type="text" placeholder="请输入内容" v-model="keyword">
				</view>
				<view class="nostrongtext" style="margin-top: 20rpx;" @click="init">搜索</view>
			</view>
		</view>
		<view class="mainpadding">
			<view class="flexcenter" v-if="!list.total">
				<view class="placeholderimage"></view>
			</view>
			<view class="mainpadding_top ffffff radius" v-for="item in list.data" :key="item.id"
				@click="selectkh(item)">
				<view class="flexbetween" style="position: relative;">
					<view class="flexleft">
						<!-- 店铺、个人图片 -->
						<view class="personicon margin_right"  v-show="item.customertype_name=='个人'"></view>
						<view class="storeicon margin_right" v-show="item.customertype_name=='店铺'"></view>
						<view class="titletext">{{item.customer_name}}</view>
					</view>
					<view class="ylable flexcenter" :style="{'background-color':item.customerlevel_bgcolor,'color':item.customerlevel_color}">{{item.customerlevel_name}}</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="dizhi">
						<view class="flexstart">
							<view class="dizhitxt">业主：{{item.connet_name}} {{item.customer_mobile}}</view>
						</view>
						<view class="flexleft">
							<view class="smalltext">地址：{{item.getgoods_address}}</view>
							<!-- <view class="addresstext">{{kmUnit(item.distance)}}</view> -->
						</view>
					</view>
					<view class="flexright">
						<view class="addressicon margin_right"  @click.stop="openmap(item)"></view>
						<view class="phoneicon" @click.stop="phone(item.customer_mobile)"></view>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				keyword: "",
				lat:"35.17466",
				lng:"109.67307",
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			getlocation(){
				let _this = this
				console.log("获取经纬度信息")
				uni.getLocation({
					type: 'gcj02',
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: function(res) {
						console.log("获取经纬度信息",res)
						_this.lat = res.latitude
						_this.lng = res.longitude
						_this.init()
					},
					fail(err){
						console.log(err,"定位错误信息")
					}
				});
			},
			gotoBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			selectkh(item) {
				uni.$emit('getkhitem',item)
				uni.navigateBack({
					delta: 1
				});
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('appapi/manage/customer/index', 'POST', {
					page: page || 1,
					limit: _this.limit,
					keyword: _this.keyword,
					lng:this.lng,
					lat:this.lat,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						// console.log(res)
						let resList = res.data.data;
						if (isPage == true) {
							_this.list.data = _this.list.data.concat(resList);
							// _this.list.data.push(...resList);
						} else {
							_this.list = res.data
							_this.list.data = resList;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			// 打开地图
			openmap(item) {
				uni.openLocation({
					latitude: Number(item.lat), //即将传到高德或腾讯地图的终点纬度  必须是数值，字符串无效
					longitude: Number(item.lng), //即将传到高德或腾讯地图的终点经度  必须是数值，字符串无效
					name: item.detail_address, //即将传到高德或腾讯地图的店铺名称
					address: item.connect_address, //即将传到高德或腾讯地图的详细地址
				})
			},
			phone(p) {
				uni.makePhoneCall({
					phoneNumber: p.toString()
				}).catch((e) => {
					// console.log(e)  //用catch(e)来捕获错误{makePhoneCall:fail cancel}
				})
			},
			// 米，公里转换
			kmUnit(m){
			    var v;
				if(typeof m === 'number' && !isNaN(m)){
					if (m >= 1000) {
			            v = (m / 1000).toFixed(2) + 'km'
			        } else {
			           	v = m + 'm'
			        }
				}else{
					v = '0m'
				}
			    return v;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header-top-box {
		height: 170rpx;
		width: 100%;
	}

	.header-top {
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		z-index: 10;
		top: 0;
		left: 0;
		background-color: #fff;
		padding: 96rpx 40rpx 10rpx;
	}

	.top-image {
		margin-top: 20rpx;
		width: 20rpx;
		height: 38rpx;
		background: url('@/static/image/system/return.png') no-repeat;
		background-size: 20rpx 38rpx;
	}

	// 自定义顶部搜索
	.searchinput {
		margin-top: 20rpx;
		border-radius: 50rpx;
		padding: 12rpx 20rpx;

		input {
			width: 100%;
		}
	}

	.searchimg {
		width: 24rpx;
		height: 24rpx;
		background-image: url("@/static/image/system/search.png");
		background-size: 100% 100%;
		margin-right: 30rpx;
	}

	//内容开始
	.userimg {
		width: 84rpx;
		height: 84rpx;
		margin-right: 30rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.boy {
		position: absolute;
		bottom: -8rpx;
		left: 50%;
		transform: translate(-50%, 0%);
		width: 16rpx;
		height: 16rpx;
		background-image: url("@/static/image/news/boy.png");
		background-size: 100% 100%;
	}

	.girl {
		position: absolute;
		bottom: -8rpx;
		left: 50%;
		transform: translate(-50%, 0%);
		width: 16rpx;
		height: 16rpx;
		background-image: url("@/static/image/news/girl.png");
		background-size: 100% 100%;
	}

	.userinfo {
		width: 80%;
		font-size: 20rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	.ylable {
		position: absolute;
		right: -30rpx;
		top: -30rpx;
		padding: 10rpx 30rpx;
		font-size: 20rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #C8101C;
		background-color: #FAE1E1;
		line-height: 23rpx;
		border-bottom-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}
	.smalltext {
		width: 300rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #737373;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	
	.dizhi {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #737373;
	}
</style>
